<?php 
	class Categories_model extends CI_Model
	{
		private $table = "no_categories";

		/*
		*
		*	Tinh tổng số bài viết hiện có
		*
		*/
		public function admin_count()
		{
			$this->db->select('*');
			$query = $this->db->get($this->table);
			
			$result = $query->result_array();
			$query->free_result();
			
			if(!empty($result))
			{
				return count($result);
			}else{
				return false;
			}
		}
		
		public function admin_save($file_name)
		{
			$id = intval($this->input->post('hidden_id'));
			if($this->input->post('status')=='on'){
				$status = 1;
			}else{
				$status = 0;
			}
			if($id == 0)
			{
				$data_content = array( 
						'parent_id' 		=> $this->input->post('parent_id'),
						'thumb'				=> $file_name,
						'name' 				=> $this->input->post('name'),
						'slug' 				=> $this->input->post('slug'),
						'created_time' 		=> getNow(),
						'modified_time' 	=> getNow(),
						'order' 			=> $this->input->post('order'),
						'status' 			=> $status
				);
			
				if($this->db->insert($this->table,$data_content)){
					//$this->update_count($id);
					return true;
				}else{
					return false;
				}
			}else{
				$data_content = array( 
						'parent_id' 			=> $this->input->post('parent_id'),
						'name' 					=> $this->input->post('name'),
						'thumb'				=> $file_name,
						'slug' 					=> $this->input->post('slug'),
						'modified_time' 		=> getNow(),
						'order' 				=> $this->input->post('order'),
						'status' 				=> $status
				);
				$this->db->where('id',$id);
				if($this->db->update($this->table,$data_content)){
					return true;
				}else{
					return false;
				}
			}
		}
		
		/*
		*	
		*	Load bài viết với start và limit
		*
		*/
		public function admin_index($start = 0,$limit = 10)
		{
			$this->db->select('*');
			$this->db->order_by('parent_id','ASC');
			$this->db->limit($start,$limit);
			$query = $this->db->get($this->table);
			
			$result = $query->result_array();
			$query->free_result();
			
			if(!empty($result))
			{
				return $result;
			}else{
				return false;
			}
		}
		
		/*
		*
		*	Load bài viết với ID truyền vào
		*
		*/
		public function admin_update($id = 0)
		{
			$id = intval($id);
			$this->db->select('*');		
			$this->db->where('id',$id);
						
			$query = $this->db->get($this->table);

			$result = $query->result_array();
			$query->free_result();
			
			if(!empty($result))
			{
				return $result;
			}else{
				return false;
			}			
		}
		
		/*
		*
		*	Dùng để xóa một bài viết
		*
		*/
		public function admin_del($product_id = 0)
		{
			$product_id = intval($product_id);

			$this->db->where('id',$product_id);
			if($this->db->delete($this->table))
			{
				// Tinh lai tong so bai viet cua bai do
				//$this->update_count($post_id);
				return true;
			}
			return false;
		}
		
		/*
		*
		*	Dùng để xóa bài viết được chọn
		*
		*/
		public function admin_del_all()
		{
			$value	= array();
			$value  = $this->input->post('check_item');
			foreach($value as $val)
			{
				$this->db->where('id',$val);
				$this->db->delete($this->table);
				$array_del = array();
			}
		}
			
		public function admin_sort($sort)
		{
			$status = '';
			$incre = '';
			
			switch($sort)
			{
				case 1:
					$status = 'created_time';
					$incre  = 'DESC';
					break;
				case 2:
					$status = 'created_time';
					$incre  = 'ASC';
					break;
				case 3:
					$status = 'status';
					$incre  = '0';
					break;
				case 4:
					$status = 'status';
					$incre  = '1';
					break;
				case 5:
					$status = 'order';
					$incre  = 'ASC';
					break;
				case 6:
					$status = 'order';
					$incre  = 'DESC';
					break;
			}
			
			$this->db->select('*');
			
			if($sort == 3 || $sort == 4)
			{
				$this->db->where($status,$incre);
				$this->db->order_by('created_time','DESC');
			}
			
			$this->db->order_by($status,$incre);
			
			$query  = $this->db->get($this->table);
			
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
		
		/*
		*
		*	Update trạng thái bài viết.
		*	Cho hiện hoặc ẩn bài viết ngoài trang chủ
		*
		*/
		function admin_update_status($id,$status)
		{
			$id = (int)$id;
			$this->db->where('id',$id);
			$data = array('status'=>$status);
			if($this->db->update($this->table,$data))
			{
				return true;
			}else{
				return false;
			}
		}
		
		public function admin_search()
		{
			$keyword   = $this->input->post('keyword');

			$this->db->select('*');
			
			if($keyword != '')
			{
				$this->db->like('name',$keyword);
			}

			$this->db->order_by('created_time','DESC');
			$query  = $this->db->get($this->table);
			$result = $query->result_array();
			$query->free_result();
			
			if(!empty($result))
			{	
				return $result;
			}else{
				return false;
			}
		}
		
		public function getParentCat()
		{
			$this->db->select('*');
			$this->db->where('parent_id',0);
			$this->db->order_by('slug','DESC');
			$query = $this->db->get($this->table);
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
		
		public function getSubCat($id = 0)
		{
			$this->db->select('*');
			$this->db->where('parent_id',$id);
			$this->db->order_by('order','DESC');
			$query = $this->db->get($this->table);
			
			$result = $query->result_array();
			$query->free_result();
			
			return $result;
		}
	}
?>